package com.yanqu.road.dao.impl.notice;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.notice.NoticeRecord;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class NoticeRecordDaoImpl extends TempDataObjectDao<NoticeRecord> {

    public Map<Long, Map<Integer, NoticeRecord>> getNoticeRecordMap() {
        String getNoticeRecordSql = "select * from t_u_notice_record";
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, Map<Integer, NoticeRecord>> noticeRecordMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getNoticeRecordSql);
            rs = call.executeQuery();
            while (rs.next()) {
                NoticeRecord noticeRecord = parse(rs);
                if (!noticeRecordMap.containsKey(noticeRecord.getUserId())) {
                    noticeRecordMap.put(noticeRecord.getUserId(), new ConcurrentHashMap<>());
                }
                noticeRecordMap.get(noticeRecord.getUserId()).put(noticeRecord.getNoticeType(), noticeRecord);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return noticeRecordMap;
    }

    @Override
    protected boolean updateData(NoticeRecord noticeRecord) {
        String sql = "update t_u_notice_record set notifyTime=? where userId=? and noticeType=?";
        PreparedStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, noticeRecord.getNotifyTime());
            call.setLong(i++, noticeRecord.getUserId());
            call.setInt(i++, noticeRecord.getNoticeType());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean addData(NoticeRecord noticeRecord) {
        String insertNoticeRecordSql = "insert into t_u_notice_record(userId,noticeType,notifyTime) values (?,?,?)";
        PreparedStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(insertNoticeRecordSql);
            int i = 1;
            call.setLong(i++, noticeRecord.getUserId());
            call.setInt(i++, noticeRecord.getNoticeType());
            call.setLong(i++, noticeRecord.getNotifyTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected NoticeRecord parseData(ResultSet rs) throws SQLException {
        NoticeRecord noticeRecord = new NoticeRecord();
        noticeRecord.setUserId(rs.getLong("userId"));
        noticeRecord.setNoticeType(rs.getInt("noticeType"));
        noticeRecord.setNotifyTime(rs.getLong("notifyTime"));
        return noticeRecord;
    }

}
